package com.dmwork.service.work.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.service.work.entity.Work;
import com.dmwork.service.work.entity.vo.WorkDetailInfoVo;
import com.dmwork.service.work.entity.vo.admin.AdminWorkBaseInfoVo;
import com.dmwork.service.work.entity.vo.admin.AdminWorkQueryVo;
import com.dmwork.service.work.feign.WorkCollectFeignService;
import com.dmwork.service.work.mq.sender.MessageSender;
import com.dmwork.service.work.service.WorkService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@RestController
@RequestMapping("/admin/dm_work/work")
public class AdminWorkController {

    @Autowired
    private WorkService workService;

    @Autowired
    private WorkCollectFeignService workCollectFeignService;

    @Autowired
    private MessageSender messageSender;


    @ApiOperation("获取作品信息列表")
    @GetMapping("get_list/{page}/{limit}")
    public R getList(@PathVariable Integer page, @PathVariable Integer limit, AdminWorkQueryVo workQueryVo) {
        Page<AdminWorkBaseInfoVo> workBaseInfoVoPage = workService.getAdminWorkBaseInfoPage(page, limit, workQueryVo);
        if (workBaseInfoVoPage.getTotal() == 0) {
            return R.ok().message("暂无数据").data("total", 0);
        } else {
            List<String> workIdList=new ArrayList<>(workBaseInfoVoPage.getRecords().size());
            for (AdminWorkBaseInfoVo workBaseInfoVo : workBaseInfoVoPage.getRecords()) {
                workIdList.add(workBaseInfoVo.getId());
            }
            Map<String, Integer> collectCountMap = workCollectFeignService.getBatchCollectCountByWorkIdList(workIdList);
            for (AdminWorkBaseInfoVo workBaseInfoVo : workBaseInfoVoPage.getRecords()) {
                workBaseInfoVo.setCollectCount(collectCountMap.get(workBaseInfoVo.getId()));
                workBaseInfoVo.setTitle(EmojiParser.parseToAliases(workBaseInfoVo.getTitle()));
            }
            return R.ok().data("total", workBaseInfoVoPage.getTotal()).data("rows", workBaseInfoVoPage.getRecords());
        }
    }

    @ApiOperation("获取指定作品信息详情")
    @GetMapping("get_info/{workId}")
    public R getInfo(@PathVariable String workId) {
        WorkDetailInfoVo workDetailInfoVo = workService.getWorkDetailInfoById(workId);
        //转换作品中标题和简介的Emoji
        workDetailInfoVo.setTitle(EmojiParser.parseToUnicode(workDetailInfoVo.getTitle()));
        workDetailInfoVo.setDescription(EmojiParser.parseToUnicode(workDetailInfoVo.getDescription()));
        return R.ok().data("item", workDetailInfoVo);
    }

    @ApiOperation("删除指定作品")
    @DeleteMapping("remove_work/{workId}")
    public R removeWork(@PathVariable String workId) {
        Work work = workService.getById(workId);
        //删除作品
        workService.removeWork(workId);
        messageSender.sendAdminRemoveWorkSystemMessage(work.getCustomerId(), work.getTitle(), new HashMap<>());
        return R.ok().message("删除成功");
    }

}
